package com.keyinong.caizhengting.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.keyinong.caizhengting.activity.DingdandetailActivity;
import com.keyinong.caizhengting.activity.FanqiedingcanActivity;
import com.keyinong.caizhengting.entity.MyorderEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class LeaderAdapter extends BaseAdapter {
    Context context;
    ArrayList<MyorderEntity> myorderList;
    HttpTools httpTools = null;

    public LeaderAdapter(Context context, ArrayList<MyorderEntity> myorderList) {
        super();
        this.context = context;
        this.myorderList = myorderList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myorderList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return myorderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHandle handle;
        if (view == null) {
            handle = new ViewHandle();
            view = LayoutInflater.from(context).inflate(R.layout.item_dingdan, null);
            handle.tv_xiadanshijian = (TextView) view.findViewById(R.id.tv_xiadanshijian2);
            handle.tv_state = (TextView) view.findViewById(R.id.tv_zhuangtai2);
            handle.tv_fenshu = (TextView) view.findViewById(R.id.tv_fenshu2);
            handle.btn_shanchu = (Button) view.findViewById(R.id.btn_shanchu2);
            handle.btn_xiangqiang = (Button) view.findViewById(R.id.btn_dingdanxiangqing2);
            handle.btn_zailaiyidan = (Button) view.findViewById(R.id.btn_zailaiyidan2);
            handle.btn_quzhifu = (Button) view.findViewById(R.id.btn_quzhifu2);
            view.setTag(handle);
        } else {
            handle = (ViewHandle) view.getTag();
        }
        handle.tv_xiadanshijian.setText(myorderList.get(position).getOrder_time());
        handle.tv_fenshu.setText(myorderList.get(position).getShuliang() + "份");
        if (myorderList.get(position).getState().equals("-1")) {
            handle.tv_state.setText("已取消");
            handle.btn_zailaiyidan.setVisibility(View.VISIBLE);
            handle.btn_quzhifu.setVisibility(View.INVISIBLE);
        } else if (myorderList.get(position).getState().equals("0")) {
            handle.tv_state.setText("待支付");
            handle.btn_quzhifu.setVisibility(View.VISIBLE);
            handle.btn_zailaiyidan.setVisibility(View.INVISIBLE);
        } else if (myorderList.get(position).getState().equals("1")) {
            handle.tv_state.setText("待确认");
            handle.btn_zailaiyidan.setVisibility(View.VISIBLE);
            handle.btn_quzhifu.setVisibility(View.INVISIBLE);
        } else if (myorderList.get(position).getState().equals("2")) {
            handle.tv_state.setText("待取餐");
            handle.btn_zailaiyidan.setVisibility(View.VISIBLE);
            handle.btn_quzhifu.setVisibility(View.INVISIBLE);
        } else if (myorderList.get(position).getState().equals("3")) {
            handle.tv_state.setText("已完成");
            handle.btn_zailaiyidan.setVisibility(View.VISIBLE);
            handle.btn_quzhifu.setVisibility(View.INVISIBLE);
        }
        /**再来一单*/
        handle.btn_zailaiyidan.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, FanqiedingcanActivity.class);
                context.startActivity(intent);
            }
        });
        /**去支付*/
        handle.btn_quzhifu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("price", myorderList.get(position).getZongjia());
                intent.putExtra("orderid", myorderList.get(position).getId());
                if (myorderList.get(position).getPaymodeid().equals("2")) {
                    Toast.makeText(context, "微信支付", Toast.LENGTH_LONG).show();
                } else {
//					intent.setClass(context, PayDemoActivity.class);
                    context.startActivity(intent);
                }

            }
        });
        /**订单详情*/
        handle.btn_xiangqiang.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, DingdandetailActivity.class);
                intent.putExtra("orderid", myorderList.get(position).getId());
                context.startActivity(intent);
            }
        });
        /**删除记录*/
        handle.btn_shanchu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                deletedingdan(position);
            }
        });
        return view;
    }

    /**
     * 删除订单
     */
    private void deletedingdan(final int index) {
        if (NetWorkUtil.isNetwork(context) == false) {
            Toast.makeText(context, "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        SharedPreferences sp = context.getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");

        if (httpTools == null) {
            httpTools = new HttpTools(context);
        }
        httpTools.deleteorder(token, myorderList.get(index).getId(),
                new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers,
                                          JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        try {
                            boolean code = response.getBoolean("code");
                            String msg = response.getString("msg");
                            if (code == true) {
                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                myorderList.remove(index);
                                notifyDataSetChanged();
                            } else {
                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                });
    }

    class ViewHandle {
        /**
         * 下单时间
         */
        TextView tv_xiadanshijian;
        /**
         * 订餐状态
         */
        TextView tv_state;
        /**
         * 订餐数量
         */
        TextView tv_fenshu;
        /**
         * 删除
         */
        Button btn_shanchu;
        /**
         * 订单详情
         */
        Button btn_xiangqiang;
        /**
         * 再来一单
         */
        Button btn_zailaiyidan;
        /**
         * 去支付
         */
        Button btn_quzhifu;
    }
}
